package com.dev.stream.For;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 戴 骥
 * @date 2021-09-05 16:34
 */
public class FindStream {
    @Test
    public void findStream() {
        List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
        List<Integer> list2 = Arrays.asList(7, 6, 9, 3, 8, 2, 1);

        // 遍历输出符合条件的元素
        list.stream().filter(t -> t == 8).forEach(System.out::println);
        List<Integer> collect = list.stream().filter(t -> t > 7).collect(Collectors.toList());
        System.out.println("collect = " + collect);
        System.out.println("==========");
        //遍历大于 6的元素  先找到大于6的元素 再组装成新的集合之后再遍历
        list.stream().filter(t -> t > 6).forEach(System.out::println);
        List<Integer> newList = list.stream().filter(t -> t > 6).collect(Collectors.toList());
        newList.forEach(System.out::println);

        // 匹配第一个
        Optional<Integer> first = list.stream().findFirst();
        Optional<Integer> first1 = list.stream().filter(t -> t > 3).findFirst();
        System.out.println("first1 = " + first1);
        System.out.println("first.getClass() = " + first.getClass());

        // 匹配任意 适合于并行流（适用于并行流） 从大于6里面随机取一个
        Optional<Integer> any = list.stream().filter(t -> t > 6).findAny();
        System.out.println("any = " + any.get());

    }
}
